/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ui.user.personal;

import hibernate.entity.Accounts;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;
import ui.common.AbstractUIBean;
import util.Md5Generator;

/**
 *
 * @author Neo
 */
@ManagedBean
@RequestScoped
public class ChangePassBean extends AbstractUIBean {

    String oldpass;
    String newpass;
    String confirmpass;

    public String getOldpass() {
        return oldpass;
    }

    public void setOldpass(String oldpass) {
        this.oldpass = oldpass;
    }

    public String getNewpass() {
        return newpass;
    }

    public void setNewpass(String newpass) {
        this.newpass = newpass;
    }

    public String getConfirmpass() {
        return confirmpass;
    }

    public void setConfirmpass(String confirmpass) {
        this.confirmpass = confirmpass;
    }

    /**
     * Creates a new instance of ChangePassBean
     */
    public ChangePassBean() {
    }

    public String doChangePass() {
        Accounts a = getCurrentAccount();
        try {
            if (!Md5Generator.getMd5(oldpass).equals(a.getPassword())) {
                return null;
            }
            Accounts acc = getCurrentAccount();
            String hash = Md5Generator.getMd5(newpass);
            acc.setPassword(hash);
            hbTransaction = hbSession.beginTransaction();
            acc.setStatus(1);
            acc = (Accounts) hbSession.merge(acc);
            hbSession.update(acc);
            hbTransaction.commit();
            HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
            session.invalidate();
            return "userlogout";
        } catch (Exception e) {
            return null;
        }

    }
}
